package com.ruoyi.business.task;

import com.ruoyi.business.domain.BusinessTradeRecord;
import com.ruoyi.business.dto.PSIceOrderStatus;
import com.ruoyi.business.dto.PSIceTransStatus;
import com.ruoyi.business.enumType.PSIceOrderState;
import com.ruoyi.business.listener.AutoOrderSubscriber;
import com.ruoyi.business.service.IBusinessTradeConfigService;
import com.ruoyi.business.service.IBusinessTradeRecordService;
import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.DateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.text.ParseException;
import java.util.Date;
import java.util.List;
import java.util.Objects;
import java.util.stream.Collectors;

@Component("businessTradeTask")
public class BusinessTradeTask {


    /**
     * 日志
     */
    private static final Logger logger = LoggerFactory.getLogger(BusinessTradeTask.class);


    @Autowired
    private IBusinessTradeRecordService businessTradeRecordService;


    /**
     * 同步当日交易订单状态
     */
    public void syncTradeOrderStatus() {
        logger.info("定时任务执行单据状态同步开始");

        try {
            List<BusinessTradeRecord> businessTradeRecordList = businessTradeRecordService.selectTodayTradeRecord();
            businessTradeRecordList= businessTradeRecordList.stream().filter(item->!PSIceOrderState.Filled.equals(PSIceOrderState.valueOf(item.getDelegateState()))).collect(Collectors.toList());
            if (!businessTradeRecordList.isEmpty()) {
                for (BusinessTradeRecord businessTradeRecord : businessTradeRecordList) {
                    // 委托状态
                    PSIceOrderStatus psIceOrderStatus = businessTradeRecordService.selectCurrentDelegateTradeOrderByOrderNo(businessTradeRecord.getOrderNo(),businessTradeRecord.getClientName(),businessTradeRecord.getPassword());

                    // 成交状态
                    PSIceTransStatus psIceTransStatus = businessTradeRecordService.selectCurrentTransTradeOrderByOrderNo(businessTradeRecord.getOrderNo(),businessTradeRecord.getClientName(),businessTradeRecord.getPassword());


                    if (Objects.isNull(psIceOrderStatus)) {
                        logger.error("定时任务同步交易订单，查询委托单据为空。 orderNo:{}", businessTradeRecord.getOrderNo());
                        continue;
                    }

                    if (Objects.isNull(psIceTransStatus)) {
                        logger.error("定时任务同步交易订单，查询交易单据为空。 orderNo:{}", businessTradeRecord.getOrderNo());
                    } else {

                    }
                    businessTradeRecord.setDealPrice(new BigDecimal(psIceOrderStatus.FilledPrice).divide(new BigDecimal(10000), 5, RoundingMode.HALF_UP));
                    businessTradeRecord.setDealVolume(psIceOrderStatus.FilledVolume);
                    businessTradeRecord.setDealAmount(new BigDecimal(psIceOrderStatus.FilledAmount).divide(new BigDecimal(10000), 5, RoundingMode.HALF_UP));
                    businessTradeRecord.setDelegateCancle(psIceOrderStatus.IsCancel);
                    businessTradeRecord.setDelegateCancleVolume(psIceOrderStatus.CancelVolume);
                    businessTradeRecord.setDelegateState(psIceOrderStatus.OrderState.value());
                    if (psIceOrderStatus.IsCancel && (psIceOrderStatus.OrderState.equals(PSIceOrderState.Canceled))) {
                        businessTradeRecord.setPositionClosed(true);
                    }
                    businessTradeRecordService.updateBusinessTradeRecord(businessTradeRecord);
                }
                logger.info("定时任务执行单据状态同步结束");
            }
        } catch (Exception e) {
            e.printStackTrace();
            logger.error("定时任务执行单据状态同步失败：", e);
        }

    }
}
